/*
 * @Author: your name
 * @Date: 2022-04-13 20:48:37
 * @LastEditTime: 2022-04-14 20:34:17
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \drivingTow\src\usege\usege.service.ts
 */
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateUsegeDto, UsegeInfoDto, UsegeRo } from './dto/create-usege.dto';
import { UpdateUsegeDto } from './dto/update-usege.dto';
import { Usege } from './entities/usege.entity';

@Injectable()
export class UsegeService {
  constructor(
    @InjectRepository(Usege)
    private readonly postsRepository:Repository<Usege>
  ){}
  // 创建教练车使用记录表
  async createUsege(post:CreateUsegeDto):Promise<Usege>{
    const newPost:Usege = await this.postsRepository.create({
      ...post
    });
    const created = await this.postsRepository.save(newPost)
    return created
  }
  
  // 查看所有的教练车使用记录
  async getAllUsege(post):Promise<UsegeRo>{
    const qb = this.postsRepository
      .createQueryBuilder('Usege')
      .where('1=1')
    const Total = await qb.getCount();
    const { pageSize,pageNum } = post
    qb.limit(pageSize)
    qb.offset(pageSize * (pageNum - 1))
    let Usege = await qb.getMany();
    const result:UsegeInfoDto[] = Usege.map(item => item.toResponseObject())
    return { list:result,Total:Total}
  }

  // 教练员查看自己教练车使用记录
  async getUsegeByCoach(post):Promise<UsegeRo>{
    const qb = this.postsRepository
      .createQueryBuilder('Usege')
      .where('Usege.coach=:id',{coach:post.id})
    const Total = await qb.getCount()
    const { pageSize,pageNum } = post
    qb.limit(pageSize)
    qb.offset(pageSize * (pageNum - 1))
    const Usege = await qb.getMany();
    const result:UsegeInfoDto[] = Usege.map(item => item.toResponseObject())
    return {list:result,Total:Total}
  }

  // 取消今天的使用
  async delectUsege(id):Promise<any>{
    const Usege = await this.postsRepository.findOne(id)
    return await this.postsRepository.remove(Usege)
  }
}
